﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using Lime49.WPF;
using LockCrypt.Core;
using LockCrypt.Core.AutoType;

namespace LockCrypt {
    public class Account : AccountBase, INotifyPropertyChanged {
        public override string ID { 
            get { return base.ID; }
            set { 
                base.ID = value;
                OnPropertyChanged("ID");
            }
        }
        public override string Name {
            get { return base.Name; }
            set {
                base.Name = value;
                OnPropertyChanged("Name");
            }
        }
        public override string ParentID {
            get { return base.ParentID; }
            set {
                base.ParentID = value;
                OnPropertyChanged("ParentID");
            }
        }
        public override ITemplate Template {
            get { return base.Template; }
            set {
                base.Template = value;
                OnPropertyChanged("Template");
            }
        }
        public override DateTime Created {
            get { return base.Created; }
            set {
                base.Created = value;
                OnPropertyChanged("Created");
            }
        }
        public override DateTime Modified {
            get { return base.Modified; }
            set {
                base.Modified = value;
                OnPropertyChanged("Modified");
            }
        }
        public override string Icon {
            get { return base.Icon; }
            set {
                base.Icon = value;
                OnPropertyChanged("Icon");
            }
        }
        public override IList<IField> Fields {
            get { return base.Fields; }
            set {
                base.Fields = value;
                OnPropertyChanged("Fields");
            }
        }
        public override string Notes {
            get { return base.Notes; }
            set {
                base.Notes = value;
                OnPropertyChanged("Notes");
            }
        }
        public override AutoTypeConfiguration AutoTypeConfig {
            get { return base.AutoTypeConfig; }
            set {
                base.AutoTypeConfig = value;
                OnPropertyChanged("AutoTypeConfig");
            }
        }

        /// <summary>
        /// Initializes a new <see cref="Account"/>.
        /// </summary>
        public Account()
            : base() {
            //Fields = new ObservableSortedDictionary<string, IField>(new Comparers.FieldComparer(), new Comparers.FieldEqualityComparer());
            Fields = new SortedObservableCollection<IField>(new Comparers.FieldComparer());
        }

        /// <summary>
        /// Initializes an <see cref="Account"/> from another account.
        /// </summary>
        /// <param name="account">The base account.</param>
        public Account(IAccount account)
            : this() {
            this.AutoTypeConfig = account.AutoTypeConfig;
            this.Created = account.Created;
            this.Icon = account.Icon;
            this.ID = account.ID;
            this.Modified = account.Modified;
            this.Name = account.Name;
            this.Notes = account.Notes;
            this.ParentID = account.ParentID;
            this.Template = new Template(account.Template);
            //foreach(KeyValuePair<string, IField> field in account.Fields) {
            //  this.Fields.Add(field.Key, new Field(field.Value));
            foreach(IField field in account.Fields) {
                this.Fields.Add(new Field(field));
            }
        }

        /// <summary>
        /// Creates a deep copy of this account.
        /// </summary>
        /// <returns>A clone of this account.</returns>
        public override IAccount Clone() {
            Account clone = new Account() {
                AutoTypeConfig = this.AutoTypeConfig,
                Created = this.Created,
                Icon = this.Icon,
                ID = this.ID,
                Modified = this.Modified,
                Name = this.Name,
                Notes = this.Notes,
                ParentID = this.ParentID,
                Template = this.Template.Clone()
            };
            foreach(IField field in this.Fields) {
                clone.Fields.Add(new Field(field));
            }
            return clone;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string strPropertyName) {
            if(PropertyChanged != null) {
                PropertyChanged(this, new PropertyChangedEventArgs(strPropertyName));
            }
        }
    }
}
